package me.zhengjie.modules.banner.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.domain.LocalStorage;
import me.zhengjie.modules.banner.domain.Banner;
import me.zhengjie.modules.banner.service.BannerService;
import me.zhengjie.modules.banner.service.dto.BannerDto;
import me.zhengjie.modules.banner.service.dto.BannerQueryParam;
import me.zhengjie.modules.company.domain.Company;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Set;

/**
* @author TianI
* @date 2022-02-07
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "轮播图接口管理")
@RequestMapping("/api/banner")
public class BannerController {

    private final BannerService bannerService;
    @GetMapping("/{companyId}")
    @Log("查询首页轮播图")
    @ApiOperation("查询首页轮播图")
    public ResponseEntity getBanner(@PathVariable Long companyId){
        List<Banner> banner = bannerService.getBanner(companyId);
        return new ResponseEntity<>(banner,HttpStatus.OK);
    }

    @GetMapping
    @Log("查询轮播图接口")
    @ApiOperation("查询轮播图接口")
    @PreAuthorize("@el.check('banner:list')")
    public ResponseEntity query(BannerQueryParam query, Pageable pageable){
        return new ResponseEntity<>(bannerService.queryAll(query,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增轮播图接口")
    @ApiOperation("新增轮播图接口")
    @PreAuthorize("@el.check('banner:add')")
    public ResponseEntity create(@Validated @RequestBody BannerDto resources){
        return new ResponseEntity<>(bannerService.insert(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改轮播图接口")
    @ApiOperation("修改轮播图接口")
    @PreAuthorize("@el.check('banner:edit')")
    public ResponseEntity update(@Validated @RequestBody BannerDto resources){
        bannerService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除轮播图接口")
    @ApiOperation("删除轮播图接口")
    @PreAuthorize("@el.check('banner:del')")
    public ResponseEntity delete(@RequestBody Set<Long> ids) {
        bannerService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("/upload")
    public ResponseEntity<Object> importProcessData(@RequestParam("companyId") Long companyId,@RequestParam("file") MultipartFile file) {
        Banner banner = bannerService.create(companyId,file);
        return new ResponseEntity<>(banner.getBannerUrl(),HttpStatus.CREATED);
    }

    /*
    @Log("导出轮播图接口")
    @ApiOperation("导出轮播图接口")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('banner:list')")
    public void download(HttpServletResponse response, BannerQueryParam query) throws IOException {
        bannerService.download(bannerService.queryAll(query), response);
    }*/

}
